import re
import random
import os

class gams:
	filename = None
	def __init__(self):
		pass
	
	def list_to_string(self, list):
		print "the list is" , list
		text = ""
		i = 0
		text += list[i]
		i += 1
		while i < len(list):
			k = str(list[i])
			text += ", " + k 
			i += 1
		return text 

	def generate_gams(self, arr, producer,consumer, gamsfilename, resultfilename, size, demand):
		if gamsfilename: 
			w = open(gamsfilename, 'w')
			self.filename = gamsfilename
		else:
			w = open("input.gams ", 'w')
			self.filename = "input.gams"
		text = ""
  		edges = {}
  		edgeCount = 0
  		edgeMatrix = [None]*size
		for i in range(size):
			edgeMatrix[i] = [None]*size

  		#print edgeMatrix
  		for j in arr:
    			vert = (j.strip()).split(" ")
    			if len(vert) == 2:
      				v1 = vert[0].strip()
     				v2 = vert[1].strip()
      				if v1 not in edges.keys():
         				edges[v1] = []
      				if v2 not in edges.keys():
         				edges[v2] = []
      			edges[v1].append(v2)
      			#edges[v2].append(v1)
      			edgeMatrix[int(v1)-1][int(v2)-1] = 1
      			edgeMatrix[int(v2)-1][int(v1)-1] = -1
      			#edges[v2].append(v1)
  		for i in producer: 
    			for j in producer:
      				if int(i) != int(j): 
        				#print i, j 
        				edgeMatrix[int(i)][int(j)] = 0
        				edgeMatrix[int(j)][int(i)] = 0
 		#print edgeMatrix
  		#print "Sets"
  		w.write("Sets")
  		i = 0
  		vertex = []
  		for k in edges.keys():
    			vertex.append(str(i))
    			i += 1

  		text = "\n\tNODES Nodes in a graph / " + self.list_to_string(vertex) +" /"
  		#print text   
  		w.write(text)
  		text = "\n\tCONSUMER(NODES) Consumer in the graph / " + self.list_to_string(consumer) +" /"
  		#print text   
  		w.write(text)
  		text = "\n\tPRODUCER(NODES) Producer in the grpah / " + self.list_to_string(producer) +" /"
  		#print text   
  		w.write(text)
  		adjlist = [] 
		print "THE edges" , edges
  		for k in edges.keys():
    			text = ""
    			if len(edges[k]) > 0:
      				text += str(int(k)-1) + ".("
      			j = 0
			print k, j
      			text +=  str( int(edges[k][j]) - 1)
     	 		j += 1
      			while j < len(edges[k]):
        			text += ", " + str(int(edges[k][j])-1)
        			j += 1
      				text += ")"
      			adjlist.append(text)
  		text = "\n\tADJ(NODES, NODES) adjacency list / " + self.list_to_string(adjlist) +" /;"
  		#print text   
  		w.write(text)
  		text = "\n\tALIAS(NODES, I);"
  		#print text   
  		w.write(text)
  		text = "\n\tALIAS(NODES, J);"
  		#print text   
  		w.write(text)
  		text = "\nTABLE COSTS(NODES, NODES) cost and adj Matrix"
  		#print text   
  		w.write(text)
  		text = "\n\t\t" 
  		k = 0
  		while  k < (len(edges.keys())):
    			text += str(k) + "\t"
    			k += 1
  			#print text
  		w.write(text)
  		text = ""
  		i = 0
  		j = 0
  		while i < size:
    			text = "\n\t"
    			text += str(i)
    			j = 0
    			while j < size:
      				n = edgeMatrix[i][j]
      				if n != None:
        				text += "\t" + str(n)
      				else:
        				text += "\t"
      				j += 1
    			i += 1
    		#print text 
    		w.write(text)
  		text = "\nPARAMETER DEMAND(NODES) / " + self.list_to_string(demand) + " /;"
  		#print text 
  		w.write(text)
  		text = "\nVARIABLES \n\tZ Objective Values\n\n\tF(NODES,NODES) The flow ;"
  		#print text
  		w.write(text)
  		text = "\nEQUATIONS\n\tOBJ     Object Function\n\tFLOWBAL(NODES) Flow Balance Constraints ;"
  		#print text
  		w.write(text)
  		text = "\nOBJ .. Z =E= SUM((I,J)$ADJ(I,J), COSTS(I,J)*F(I,J));\n\tFLOWBAL(I) .. SUM( J$ADJ(J,I), F(J,I)) - SUM( J$ADJ(I,J), F(I,J))\
				=E= DEMAND(I);\n\tMODEL DIST / ALL /;\nSOLVE DIST USING LP MINIMIZING Z ;\nDISPLAY F.l;"
  		#print text
  		w.write(text)
  		text ="FILE soln  /" +  resultfilename + " / ;\nsoln.pc = 3\nPUT soln;\nLOOP((I,J),\n\tPUT I.tl , J.tl , F.l(I,J) /\n\t);"
  		#print text
  		w.write(text)
  		text = "\nPUTCLOSE;"
  		#print text
  		w.write(text)
  		text = "\nOPTION"
  		#print text
  		w.write(text)
  		text = "\n\tsolprint = off,\n\tsysout = off,\n\tLIMROW = 0,\n\tLIMCOL = 0;"
  		#print text
  		w.write(text)


  


arr = ["0 1", "0 2", "0 5","1 0", "1 5", "2 0" , "2 3", "3 2",  "3 4", "4 3", "5 0", "5 1"]
producer = ['1', '2', '3']
consumer = ['0', '5']
#demand = 
